<?php
$this->headTitle('Đối soát game '.$this->gameCheck['gameName'].' ('.$this->gameCheck['gameId'].')', 'append');
?>

<div class="grid">
    <div class="breadcrumbs">
        <a class="item-1 loading" href="/index">Trang chủ CI</a> › 
        <a class="item-2 loading" href="/transaction/index/game/<?php echo $this->gameCheck['gameId'];?>"><?php echo $this->gameCheck['gameName'],' (',$this->gameCheck['gameId'],')';?></a> › 
        <span class="blue">Đối soát</span>
        <div class="clear-both"></div>
    </div>
    <?php if(isset($this->repair)){ ?>
    
            <?php
                if($this->flag == 'success')
                    echo '<div class="notification success">',$this->mess,'</div>';
                elseif($this->flag == 'alert')
                    echo '<div class="notification alert">',$this->mess,'</div>';
                else
                    echo '<div class="notification error">',$this->mess,'</div>';
            ?>
    <?php }else{ ?>
    <div class="flugin">
        <div class="search">
            <div class="form">
                <div id="tabs-search">
                    <ul class="tabs-menu">
                        <li class="active" id="tabs-1"><span>Tìm kiếm</span></li>
                    </ul>
                    <div class="tabs tabs-1">
                        <form name="fSeachDate" action="/transaction/index/game/<?php echo $this->gameCheck['gameId'];?>" method="GET" onsubmit="return checkSearchTransaction();">
                            <div class="name">
                                <label>Tài khoản</label>
                                <input type="text" name="username" value="<?php echo (isset($this->username)) ? $this->username : '';?>"/>
                            </div>
                            <div class="status">
                                <label>Trạng thái</label>
                                <select name="status">
                                    <option value="-1">Tất cả</option>
                                    <option value="1" <?php echo (isset($this->status) && $this->status == 1) ? 'selected="selected"' : '';?>>Thành công</option>
                                    <option value="0" <?php echo (isset($this->status) && $this->status == 0) ? 'selected="selected"' : '';?>>Thất bại</option>
                                </select>
                            </div>
                            <div class="date">
                                <label>Từ ngày</label>
                                <input type="text" name="dateStart" readonly="readonly" value="<?php echo (isset($this->dateStart)) ? $this->dateStart : '';?>" class="datepicker2"/>
                                <div class="clear-both"></div>
                                <div class="dateEnd">
                                    <label>Đến ngày</label>
                                    <input type="text" name="dateEnd" readonly="readonly" value="<?php echo (isset($this->dateEnd)) ? $this->dateEnd : '';?>" class="datepicker2"/>
                                </div>
                            </div>
                            <div class="type">
								<?php if(!isset($this->configGame->transaction->agent_id) || (isset($this->configGame->transaction->agent_id) && $this->configGame->transaction->agent_id == 1)){ ?>
                                <label>Channel</label>
                                <select name="channel" class="channel">
                                    <option value="0">Vui lòng chọn</option>
                                    <?php 
                                        $listChannel = '';
                                        foreach($this->arrChannel as $key=>$channel){
                                            echo '<option value="',$key,'"',(isset($this->channel) && $this->channel == $key) ? 'selected="selected"' : '','>',$channel['name'],'</option>';
                                            $listChannel .= " {$key}";
                                        } 
                                    ?>
                                </select>
                                <div class="clear-both"></div>
								<?php } ?>
								<?php if(!isset($this->configGame->transaction->server) || (isset($this->configGame->transaction->server) && $this->configGame->transaction->server == 1)){ ?>
                                <div class="server">
                                    <label>Server</label>
                                    <select name="serverId" class="serverId">
                                        <?php
                                            if(isset($this->channel)){
                                                echo '<option class="0" value="0">Tất cả</option>';
                                                foreach($this->arrChannel[$this->channel] as $key=>$channel){
                                                    foreach($channel as $row){
                                                        echo '<option value="',$row,'"',(isset($this->serverId) && $this->serverId == $row) ? 'selected="selected"' : '','>',$this->arrServer[$row],'</option>';
                                                    } 
                                                } 
                                            }else{
                                                echo '<option class="0" value="0">Tất cả</option>';
                                                if(isset($this->arrServer) && is_array($this->arrServer)){
                                                    foreach($this->arrServer as $key=>$server){
                                                        echo '<option value="',$key,'"',(isset($this->serverId) && $this->serverId == $key) ? 'selected="selected"' : '','>',$server,'</option>';
                                                                                                        
                                                    }
                                                }
                                            }
                                        ?>
                                    </select>
									
                                    <select name="serverList" class="serverList hidden">
                                        <option value="0">Vui lòng chọn Channel</option>
                                        <option class="<?php echo trim($listChannel);?> 0" value="0">Tất cả</option>
                                        <?php 
                                            foreach($this->arrServer as $key=>$row){
                                                $cls = '';
                                                foreach($this->arrChannel as $key2=>$channel){
                                                    if(in_array($key, $channel['value'])){
                                                        $cls .= ' '.$key2;
                                                    }
                                                }
                                                (strlen($cls) > 0) ? $cls = trim($cls) : $cls = '';
                                                echo '<option value="',$key,'" class="0 ',$cls,'">',$row,'</option>';
                                            } 
                                        ?>
                                    </select>
									
                                    <div class="clear-both"></div>
                                </div>
								<?php } ?>
                            </div>
                            <div class="submit">
                                <input type="submit" name="bntSubmit" value="Tìm" class="loading"/>
                            </div>
                            <div class="clear-both"></div>
                        </form>
                    </div>
                    <div class="clear-both"></div>
                </div>
                <div class="clear-both"></div>
            </div>
            <div class="clear-both"></div>
            <script type="text/javascript">
                function checkSearchTransaction(){
                    var username = document.fSeachDate.username.value;
                    var dateS = document.fSeachDate.dateStart.value;
                    var dateE = document.fSeachDate.dateEnd.value;
                    if (username==null || username=="")
                    {
                        alert("Vui lòng nhập username!");
                        $('.loading-please-wait').css('display','none');
                        return false;
                    }
                    if (dateS==null || dateS=="")
                    {
                        alert("Vui lòng chọn ngày bắt đầu!");
                        $('.loading-please-wait').css('display','none');
                        return false;
                    }
                    if (dateE==null || dateE=="")
                    {
                        alert("Vui lòng chọn ngày kết thúc!");
                        $('.loading-please-wait').css('display','none');
                        return false;
                    }
                    return true;
                }
                
                $(document).ready(function(){
                    /*
                    $(".channel").change(function() {
                        val = $(this).find("option:selected").val();
                        $(".serverId option").remove();
                        $(".serverList option."+val).clone().appendTo(".serverId");
                    });
                    */
                    $(".channel").change(function() {
                        val = $(this).find("option:selected").val();
                        $(this).parent().find(".serverId option").remove();
                        $(this).parent().find(".serverList option."+val).clone().appendTo(".serverId");
                    });
                })
            </script>
        </div>
        <?php
            if($this->mess){
                if($this->flag == 'success')
                    echo '<div class="notification success">',$this->mess,'</div>';
                elseif($this->flag == 'alert')
                    echo '<div class="notification alert">',$this->mess,'</div>';
                else
                    echo '<div class="notification error">',$this->mess,'</div>';
            }
        ?>
        <?php if($this->data){?>
        <div class="page">
            <a class="export" href="<?php echo $this->url(array('action' => 'export', 'keyMem' => $this->keyMem)); ?>"><img style="margin: 3px 0 0 0;" src="/image/icons/icon_excel.png" title="Xuất file excel" alt="Xuất file excel"/></a>
        </div>
        <div class="statistic">
            <strong>Tổng transaction</strong>: <?php echo number_format($this->totalRecord,0,'.',',');?> lượt&nbsp;&nbsp;I&nbsp;&nbsp;
            <strong>Tiền trong game</strong>: <?php echo number_format($this->totalKnb,0,'.',',');?>
            <?php if(isset($this->configGame->transaction->gold) && $this->configGame->transaction->gold != 0){ ?>
                &nbsp;&nbsp;I&nbsp;&nbsp;<strong>Tiền convert</strong>: <?php echo number_format($this->totalGold,0,'.',',');?>
            <?php } ?>
        </div>
        <div class="table">
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <thead>
                    <tr>
                        <td class="column">STT</td>
                        
                        <?php 
                            $name_trans_ID = 'Transaction ID';
                            if(isset($this->configGame->transaction->allow->view->trans_ID)){
                                $name_trans_ID = $this->configGame->transaction->allow->view->trans_ID;
                            }
                            $name_trans_Partner_ID = 'Partner Transaction ID';
                            if(isset($this->configGame->transaction->allow->view->trans_Partner_ID)){
                                $name_trans_Partner_ID = $this->configGame->transaction->allow->view->trans_Partner_ID;
                            }
                            $name_username = 'Username';
                            if(isset($this->configGame->transaction->allow->view->username)){
                                $name_username = $this->configGame->transaction->allow->view->username;
                            }
                            $name_gold = 'Tiền trong game';
                            if(isset($this->configGame->transaction->allow->view->gold)){
                                $name_gold = $this->configGame->transaction->allow->view->gold;
                            }
                            $name_gold_convert = 'Tiền convert';
                            if(isset($this->configGame->transaction->allow->view->gold_convert)){
                                $name_gold_convert = $this->configGame->transaction->allow->view->gold_convert;
                            }
                            $name_date = 'Ngày';
                            if(isset($this->configGame->transaction->allow->view->date)){
                                $name_date = $this->configGame->transaction->allow->view->date;
                            }
                            $name_server = 'Server';
                            if(isset($this->configGame->transaction->allow->view->server)){
                                $name_server = $this->configGame->transaction->allow->view->server;
                            }
                            $name_channel = 'Channel';
                            if(isset($this->configGame->transaction->allow->view->channel)){
                                $name_channel = $this->configGame->transaction->allow->view->channel;
                            }
                            $name_channel_secondary = 'Channel Secondary';
                            if(isset($this->configGame->transaction->allow->view->channel_secondary)){
                                $name_channel_secondary = $this->configGame->transaction->allow->view->channel_secondary;
                            }
                            $name_status = 'Trạng thái';
                            if(isset($this->configGame->transaction->allow->view->status)){
                                $name_status = $this->configGame->transaction->allow->view->status;
                            }
                            $name_note = 'Note';
                            if(isset($this->configGame->transaction->allow->view->note)){
                                $name_note = $this->configGame->transaction->allow->view->note;
                            }
                        ?>
                        <td class="column"><?php echo $name_trans_ID;?></td>
                        <?php if(isset($this->configGame->transaction->tranPartner) && $this->configGame->transaction->tranPartner == 1){ ?>
                        <td class="column"><?php echo $name_trans_Partner_ID;?></td>
                        <?php } ?>
                        <td class="column"><?php echo $name_username;?></td>
                        <td class="column"><?php echo $name_gold;?></td>
                        <?php if(isset($this->configGame->transaction->gold) && $this->configGame->transaction->gold != 0){ ?>
                        <td class="column"><?php echo $name_gold_convert;?></td>
                        <?php } ?>
                        <td class="column"><?php echo $name_date;?></td>
						<?php if(!isset($this->configGame->transaction->server) || (isset($this->configGame->transaction->server) && $this->configGame->transaction->server == 1)){ ?>
                        <td class="column"><?php echo $name_server;?></td>
						<?php } ?>
                        <?php if(!isset($this->configGame->transaction->agent_id) || (isset($this->configGame->transaction->agent_id) && $this->configGame->transaction->agent_id == 1)){ ?>
                        <td class="column"><?php echo $name_channel;?></td>
                        <?php } ?>
                        <?php if(!isset($this->configGame->transaction->secondary_agent_id) || (isset($this->configGame->transaction->secondary_agent_id) && $this->configGame->transaction->secondary_agent_id == 1)){ ?>
                        <td class="column"><?php echo $name_channel_secondary;?></td>
                        <?php } ?>
                        <td class="column"><?php echo $name_status;?></td>
                        <?php if(isset($this->configGame->transaction->fail) && $this->configGame->transaction->fail){ ?>
                        <td class="column"><?php echo $name_note;?></td>
                        <?php } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                        $stt = 1;
                        if($this->data){
                            foreach($this->data as $row){
                                echo '<tr>';
                                echo '<td class="text-align-center">',$stt,'</td>';
                                echo '<td class="text-align-left">',(isset($row['transaction_id'])) ? $row['transaction_id'] : '-','</td>';
                                if(isset($this->configGame->transaction->tranPartner) && $this->configGame->transaction->tranPartner == 1){
                                    echo '<td class="text-align-left">',(isset($row['agent_transaction_id'])) ? $row['agent_transaction_id'] : '-','</td>';
                                }
                                echo '<td class="text-align-left">',(isset($row['username'])) ? $row['username'] : '-','</td>';
                                echo '<td class="text-align-right">',(isset($row['knb'])) ? number_format($row['knb'],0,'.',',') : '-','</td>';
                                if(isset($this->configGame->transaction->gold) && $this->configGame->transaction->gold != 0){
                                    echo '<td class="text-align-right">',(isset($row['gold'])) ? number_format($row['gold'],0,'.',',') : '-','</td>';
                                }
                                echo '<td class="text-align-left">',(isset($row['update_date'])) ? date("Y-m-d H:i:s",strtotime($row['update_date'])) : '-','</td>';
                                if(!isset($this->configGame->transaction->server) || (isset($this->configGame->transaction->server) && $this->configGame->transaction->server == 1)){
									echo '<td class="text-align-left">',(isset($row['server_id']) && isset($this->configGame->server->$row['server_id']->name)) ? $this->configGame->server->$row['server_id']->name : $row['server_id'],'</td>';
								}
                                if(!isset($this->configGame->transaction->agent_id) || (isset($this->configGame->transaction->agent_id) && $this->configGame->transaction->agent_id == 1)){
                                    echo '<td class="text-align-left">',(isset($row['agent_id'])) ? ((isset($this->configGame->channel->$row['agent_id']->name)) ? $this->configGame->channel->$row['agent_id']->name : '') : '-','</td>';
                                }
                                if(!isset($this->configGame->transaction->secondary_agent_id) || (isset($this->configGame->transaction->secondary_agent_id) && $this->configGame->transaction->secondary_agent_id == 1)){
                                    echo '<td class="text-align-left">',(isset($row['secondary_agent_id'])) ? $row['secondary_agent_id'] : '-','</td>';
                                }
                                if(isset($row['status'])){
                                    if(isset($row['statusAll'])){
                                        if($row['statusAll'] == 1){
                                            $status = '<span class="blue">Thành công</span>';
                                        }else{
                                            if(isset($this->configGame->transaction->success)){
                                                $success = ','.$this->configGame->transaction->success.',';
                                            }else{
                                                $success = ',1,';
                                            }
                                            $checkStatus = strrpos('checkStatus_'.$row['statusAll'],$success);
                                            
                                            if($checkStatus > 1){
                                                $status = '<span class="blue">Thành công</span>';
                                            }else{
                                                $status = '<span class="red">Thất bại</span>';
                                            }
                                        }
                                    }else{
                                        if($row['status'] == 1){
                                            $status = '<span class="blue">Thành công</span>';
                                        }else{
                                            $status = '<span class="red">Thất bại</span>';
                                        }
                                    }
                                    echo '<td class="text-align-left">',$status,'</td>';
                                }
                                if(isset($this->configGame->transaction->fail) && $this->configGame->transaction->fail){
                                    $statusAll ='';
                                    if(isset($this->configGame->transaction->cancel) && $this->configGame->transaction->cancel == $row['status']){
                                        $statusAll = '<span class="red">Hủy giao dịch</span>';
                                    }elseif($status == '<span class="red">Thất bại</span>'){
                                        $arrStatusFail = $this->configGame->transaction->fail->toArray();
                                        foreach($arrStatusFail as $fail){
                                            if(strpos('start'.$row['statusAll'],',')){
                                                $check = strrpos('start'.$row['statusAll'],','.$fail['value'].',');
                                            }else{
                                                $check = strrpos('start'.$row['statusAll'],$fail['value']);
                                            }
                                            if($fail['cond'] == "=="){
                                                if($check){
                                                    $statusAll = '<span class="red">'.$fail['mess'].'</span>';
                                                }
                                            }else{
                                                if(isset($fail['sub']) && isset($fail['sub']['cond']) && isset($fail['sub']['value'])){
                                                    $check2 = strrpos('start'.$row['statusAll'],','.$fail['sub']['value'].',');
                                                    if($fail['sub']['cond'] == "=="){
                                                        if($check2){
                                                            if(!$check){
                                                                $statusAll = '<span class="red">'.$fail['mess'].'</span>';
                                                            }
                                                        }
                                                    }
                                                }elseif(!$check){
                                                    $statusAll = '<span class="red">'.$fail['mess'].'</span>';
                                                }
                                            }
                                            if(isset($fail['stop']))
                                                break;
                                            
                                        }
                                    }
                                    echo '<td class="text-align-left">',$statusAll,'</td>';
                                }
                                echo '</tr>';
                                ++$stt; 
                            }
                        }
                    ?>
                </tbody>
            </table>
        </div>
        <div class="pagination">
            <?php 
                if($this->totalRecord > 30){
                    $urlOption = array('type'=>'transaction');
                    if($this->username){
                        $urlOption['username'] = $this->username;
                    }
                    if($this->dateStart){
                        $urlOption['dateStart'] = $this->dateStart;
                    }
                    if($this->dateEnd){
                        $urlOption['dateEnd'] = $this->dateEnd;
                    }
                    if($this->channel){
                        $urlOption['channel'] = $this->channel;
                    }
                    if($this->serverId){
                        $urlOption['serverId'] = $this->serverId;
                    }
                    if(isset($this->status) && is_numeric($this->status)){
                        $urlOption['status'] = $this->status;
                    }
                    echo $this->paginationControl($this->data, 'Sliding' , 'pagination.phtml', $urlOption); 
                }
            ?>
        </div>
        <?php } ?>
    </div>
    <?php } ?>
</div>
